<template>
  <div class="WareHouse">
    <div class="search brr pdd wbg">
      <el-form
        ref="searchForm"
        label-position="top"
        :model="searchForm"
        label-width="80px"
      >
        <el-form-item label="库区名称">
          <el-input
            v-model="searchForm.areaName"
            placeholder="请输入"
          ></el-input>
        </el-form-item>
        <el-form-item label="库位名称">
          <el-input v-model="searchForm.name" placeholder="请输入"></el-input>
        </el-form-item>
        <el-form-item label="仓库状态">
          <el-select v-model="searchForm.status" clearable placeholder="请选择">
            <el-option label="全部" value="false"></el-option>
            <el-option label="停用" value="0"></el-option>
            <el-option label="启用" value="1"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" round @click="getListData">搜索</el-button>
          <el-button round @click="resSearchForm">重置</el-button>
        </el-form-item>
      </el-form>
    </div>
    <div class="list brr wbg mgt">
      <div class="listBtnBar">
        <el-button type="success" @click="jumpDetails()" round
          >新增库位</el-button
        >
        <div class="leftBtn">
          <a href="./template/locationTemplate.xlsx">
            <el-button round>下载库位模板</el-button>
          </a>
          <el-button round @click="fileUpload">导入库位信息</el-button>
        </div>
      </div>
      <el-empty
        v-loading="loading"
        v-if="!tableData.length"
        :image="$store.state.emptyImage"
        description="暂无库位"
        class="elEmpty"
      ></el-empty>
      <div v-if="tableData.length">
        <el-table
          v-loading="loading"
          :data="tableData"
          border
          stripe
          style="width: 100%"
        >
          <el-table-column align="center" type="index" label="序号" width="68">
          </el-table-column>
          <el-table-column
            align="center"
            prop="warehouseName"
            label="所属仓库"
            width="160"
          >
          </el-table-column>
          <el-table-column
            align="center"
            prop="areaCode"
            label="库区编号"
            width="160"
          >
          </el-table-column>
          <el-table-column
            align="center"
            prop="areaName"
            label="库区名称"
            width="160"
          >
          </el-table-column>
          <el-table-column
            align="center"
            prop="code"
            label="库位编号"
            width="160"
          >
          </el-table-column>
          <el-table-column
            align="center"
            prop="name"
            label="库位名称"
            width="160"
          >
          </el-table-column>
          <el-table-column align="center" label="温度类型" width="160">
            <template #default="{ row }">
              {{ temperatureType(row.temperatureType) }}
            </template>
          </el-table-column>
          <el-table-column align="center" label="承重类型" width="160">
            <template #default="{ row }">
              {{ bearingType(row.bearingType) }}
            </template>
          </el-table-column>
          <el-table-column align="center" label="用途属性" width="160">
            <template #default="{ row }">
              {{ wearhouseUseType(row.useType) }}
            </template>
          </el-table-column>
          <el-table-column align="center" label="仓库状态" width="160">
            <template #default="{ row }">
              {{ warehouseStatus(row.status) }}
            </template>
          </el-table-column>
          <el-table-column
            align="center"
            prop="maxNum"
            label="承载上限"
            width="160"
          >
          </el-table-column>
          <el-table-column
            align="center"
            prop="updateTime"
            label="更新时间"
            width="200"
          >
          </el-table-column>
          <el-table-column
            align="center"
            fixed="right"
            label="操作"
            width="180"
          >
            <template #default="{ row }">
              <el-button type="text" @click="jumpDetails(row.id)"
                >编辑</el-button
              >
              <el-button
                type="text"
                @click="warehouseSwitch(row.status, row.id, row.name)"
              >
                {{ warehouseStatus(row.status ? 0 : 1) }}
              </el-button>
              <el-button type="text" @click="warehouseDelete(row.id, row.name)"
                >删除</el-button
              >
            </template>
          </el-table-column>
        </el-table>
        <el-pagination
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :current-page="searchForm.current"
          :page-sizes="[10, 20, 30, 40]"
          :page-size="searchForm.size"
          layout="total, sizes, prev, pager, next, jumper"
          :total="paginationTotal"
        >
        </el-pagination>
      </div>
    </div>
    <el-dialog :title="dialogTitle" :visible.sync="dialogVisible" width="30%">
      <template>
        <span v-if="dialogIf"
          >确定要{{
            this.warehouseStatus(wearhouseStatus) + "：" + wearhouseName
          }}
          吗？</span
        >
        <span v-else>确定要删除 {{ wearhouseName }} 吗？</span>
      </template>
      <template #footer>
        <el-button @click="dialogVisible = false" round>取消</el-button>
        <el-button
          v-if="dialogIf"
          type="primary"
          @click="warehouseSwitchFn"
          round
          >确定</el-button
        >
        <el-button v-else type="primary" @click="warehouseDeleteFn" round
          >确定</el-button
        >
      </template>
    </el-dialog>
    <uploadFile
      ref="upload"
      :url="baseUrl + '/api/location/upload'"
      title="导入库位"
      :fileAccept="['xlsx', 'xls']"
    />
  </div>
</template>

<script>
import { baseUrl } from '@/utils/request'
import { getLocation, modifyLocation, delLocation } from '@/api/location'
import {
  warehouseStatus,
  wearhouseUseType,
  bearingType,
  temperatureType
} from '@/constant/warehouse'
import uploadFile from '@/components/fileUpload.vue'
export default {
  components: {
    uploadFile
  },
  name: 'AreaPage',
  data () {
    return {
      baseUrl,
      loading: false,
      searchForm: {
        name: '',
        areaName: '',
        status: '',
        current: 1,
        size: 10,
        descs: 'createTime'
      },
      paginationTotal: 0,
      tableData: [],
      dialogVisible: false,
      dialogTitle: '',
      dialogIf: '',
      wearhouseId: '',
      wearhouseStatus: '',
      wearhouseName: ''
    }
  },
  created () {
    this.getListData()
    console.log(baseUrl)
  },
  methods: {
    fileUpload () {
      console.log((this.$refs.upload.dialogVisible = true))
    },
    async getListData () {
      this.loading = true
      const {
        data: { data }
      } = await getLocation({
        ...this.searchForm,
        status:
          this.searchForm.status === ''
            ? null
            : this.searchForm.status === 'false'
              ? null
              : this.searchForm.status,
        name: this.searchForm.name === '' ? null : this.searchForm.name,
        areaName:
          this.searchForm.areaName === '' ? null : this.searchForm.areaName
      })
      this.tableData = data.records
      this.loading = false
      this.paginationTotal = Number(data.total)
    },
    warehouseStatus (type) {
      return (
        warehouseStatus.find((item) => item.type === type)?.status || '未知'
      )
    },
    wearhouseUseType (type) {
      return (
        wearhouseUseType.find((item) => item.type === type)?.region || '未知'
      )
    },
    bearingType (type) {
      return bearingType.find((item) => item.type === type)?.value || '未知'
    },
    temperatureType (type) {
      return (
        temperatureType.find((item) => item.type === type)?.value || '未知'
      )
    },
    resSearchForm () {
      this.searchForm = {
        like_code: '',
        like_name: '',
        status: '',
        current: 1,
        size: this.searchForm.size,
        descs: 'createTime'
      }
      this.getListData()
    },
    warehouseSwitch (status, id, name) {
      this.wearhouseStatus = status ? 0 : 1
      this.dialogTitle = '确认' + this.warehouseStatus(this.wearhouseStatus)
      this.dialogVisible = true
      this.dialogIf = true
      this.wearhouseId = id
      this.wearhouseName = name
    },
    warehouseSwitchFn () {
      modifyLocation({ id: this.wearhouseId, status: this.wearhouseStatus })
        .then(() => {
          this.$message.success('状态改变成功')
          this.getListData()
        })
        .catch(() => {
          this.$message.warning('状态改变失败')
        })
      this.dialogVisible = false
    },
    warehouseDelete (id, name) {
      this.dialogTitle = '确认删除'
      this.dialogVisible = true
      this.dialogIf = false
      this.wearhouseId = id
      this.wearhouseName = name
    },
    warehouseDeleteFn () {
      delLocation({ 'ids[]': this.wearhouseId })
        .then(() => {
          this.$message.success('删除成功')
          this.getListData()
        })
        .catch(() => {
          this.$message.warning('删除失败')
        })
      this.dialogVisible = false
    },
    jumpDetails (value = 'null') {
      const path =
        '/manage-base-info/location/details' +
        (value === 'null' ? '' : '/' + value)
      this.$router.push(path)
    },
    handleSizeChange (val) {
      this.searchForm.size = val
      this.getListData()
    },
    handleCurrentChange (val) {
      this.searchForm.current = val
      this.getListData()
    }
  }
  // activated () {
  //   this.getListData()
  //   this.resSearchForm()
  // }
}
</script>

<style lang="scss" scoped>
.WareHouse {
  .search .el-form {
    display: flex;
    .el-form-item {
      flex: 1;
      display: flex;
      flex-direction: column;
      margin: 0 30px 0 0;
      ::v-deep .el-form-item__label {
        padding: 0;
        font-size: 12px;
        line-height: 1em;
        margin-bottom: 10px;
      }
      .el-select {
        width: 100%;
      }
      &:last-child {
        align-items: flex-end;
        justify-content: flex-end;
        margin: 0;
        .el-button {
          margin-left: 20px;
        }
      }
    }
  }
}
</style>
